package com.smart.community.region.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.smart.community.commons.exception.BusinessException;
import com.smart.community.region.dto.OwnerQueryDTO;
import com.smart.community.region.entity.OwnerHousehold;
import com.smart.community.region.service.IOwnerHouseholdService;
import com.smart.community.region.service.IOwnerService;
import com.smart.community.region.service.OwnerListService;
import com.smart.community.region.vo.OwnerVO;

import lombok.extern.slf4j.Slf4j;

/**
 * 业主列表查询服务实现类
 * 负责业主列表查询优化，增加关联状态查询，支持未关联业主显示
 * 
 * @author Wu.Liang
 * @since 2025-08-22
 * @version 1.0.0
 */
@Slf4j
@Service
public class OwnerListServiceImpl implements OwnerListService {
    
    @Autowired
    private IOwnerService ownerService;
    
    @Autowired
    private IOwnerHouseholdService ownerHouseholdService;
    
    @Override
    public IPage<OwnerVO> getOwnerListWithAssociation(OwnerQueryDTO queryDTO) throws Exception {
        try {
            // 获取业主列表
            IPage<OwnerVO> ownerPage = ownerService.getOwnerPage(queryDTO);
            
            // 补充关联信息
            List<OwnerVO> ownerVOList = ownerPage.getRecords().stream()
                .map(this::enrichOwnerVOWithAssociation)
                .collect(Collectors.toList());
            
            // 更新分页结果
            ownerPage.setRecords(ownerVOList);
            
            return ownerPage;
        } catch (Exception e) {
            log.error("获取业主列表失败", e);
            throw new BusinessException("获取业主列表失败：" + e.getMessage());
        }
    }
    
    @Override
    public IPage<OwnerVO> getUnassociatedOwnerList(OwnerQueryDTO queryDTO) throws Exception {
        try {
            // 获取所有业主列表
            IPage<OwnerVO> ownerPage = ownerService.getOwnerPage(queryDTO);
            
            // 过滤出未关联房户的业主
            List<OwnerVO> unassociatedOwners = ownerPage.getRecords().stream()
                .map(this::enrichOwnerVOWithAssociation)
                .filter(vo -> vo.getAssociationStatus() == 0) // 只保留未关联的
                .collect(Collectors.toList());
            
            // 更新分页结果
            ownerPage.setRecords(unassociatedOwners);
            
            return ownerPage;
        } catch (Exception e) {
            log.error("获取未关联业主列表失败", e);
            throw new BusinessException("获取未关联业主列表失败：" + e.getMessage());
        }
    }
    
    @Override
    public IPage<OwnerVO> getAssociatedOwnerList(OwnerQueryDTO queryDTO) throws Exception {
        try {
            // 获取所有业主列表
            IPage<OwnerVO> ownerPage = ownerService.getOwnerPage(queryDTO);
            
            // 过滤出已关联房户的业主
            List<OwnerVO> associatedOwners = ownerPage.getRecords().stream()
                .map(this::enrichOwnerVOWithAssociation)
                .filter(vo -> vo.getAssociationStatus() == 1) // 只保留已关联的
                .collect(Collectors.toList());
            
            // 更新分页结果
            ownerPage.setRecords(associatedOwners);
            
            return ownerPage;
        } catch (Exception e) {
            log.error("获取已关联业主列表失败", e);
            throw new BusinessException("获取已关联业主列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 补充业主VO的关联信息
     * 
     * @param ownerVO 业主VO
     * @return 补充关联信息后的业主VO
     */
    private OwnerVO enrichOwnerVOWithAssociation(OwnerVO ownerVO) {
        try {
            // 获取关联的房户信息
            List<OwnerHousehold> associations = ownerHouseholdService.getByOwnerId(ownerVO.getId());
            
            // 设置关联状态
            if (associations.isEmpty()) {
                ownerVO.setAssociationStatus(0); // 未关联
                ownerVO.setAssociationStatusText("未关联房户");
                ownerVO.setHouseholdCount(0);
                ownerVO.setHouseholds(null);
            } else {
                ownerVO.setAssociationStatus(1); // 已关联
                ownerVO.setAssociationStatusText("已关联房户");
                ownerVO.setHouseholdCount(associations.size());
                ownerVO.setHouseholds(null); // 暂时不设置详细房户信息
            }
            
            return ownerVO;
        } catch (Exception e) {
            log.error("补充业主关联信息失败，业主ID：{}", ownerVO.getId(), e);
            // 设置默认值
            ownerVO.setAssociationStatus(0);
            ownerVO.setAssociationStatusText("查询失败");
            ownerVO.setHouseholdCount(0);
            ownerVO.setHouseholds(null);
            return ownerVO;
        }
    }
}
